package cn.yunhe.actions;

import cn.yunhe.model.House;
import cn.yunhe.model.Member;
import cn.yunhe.service.MemberBiz;
import cn.yunhe.util.BaseActionUtil;
import cn.yunhe.util.ModelDisplayUtil;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Iterator;
import java.util.List;

/*spring注解*/
@Controller(value = "memberAction")
@Scope(value = "prototype")
/*struts2注解*/
@ParentPackage(value = "struts-default")
public class MemberAction extends BaseActionUtil implements ModelDriven {

    private Member member;

    /*spring注解*/
    @Autowired
    @Qualifier("memberBiz")
    MemberBiz memberBiz;

    /*struts2注解*/
    @Action(value = "member_searchMember",results = {
            @Result(name = "success",location = "/searchMember.jsp"),
            @Result(name = "error",location = "/fail2SearchMember.jsp"),
    })
    public String searchMember(){
        //此处的Member1对应
        //@Entity(name = "Member1")
        //@Table(name = "member")
        List<Member>memberList=memberBiz.searchMember("from Member1");
        getRequest().setAttribute("memberList1",memberList);

        ModelDisplayUtil.listMember(memberList);
        return SUCCESS;
    }


    /*struts2注解*/
    @Action(value = "member_register",results = {
            @Result(name = "success",location = "/login.jsp"),
    })
    public String register(){
        member.setMstate(0);
        member.setMflag(0);
        boolean flag=memberBiz.insertMember(member);

        return SUCCESS;
    }

    /*struts2注解*/
    @Action(value = "member_login",results = {
            /*@Result(name = "success",location = "/searchHome.jsp"),*/
            @Result(name = "success",location = "/house_searchHouse1",type = "redirect"),
            @Result(name = "fail2register",location = "/register.jsp"),
    })
    public String login(){
        String hql="from Member1 where maccount='"+member.getMaccount()+"' and mpwd='"+member.getMpwd()+" '";

        List<Member>memberList =memberBiz.searchMember(hql);
        Member member=memberList.size()>0?memberList.get(0):null;
        if (member!=null){

            return SUCCESS;
        }else{
            return "fail2register";
        }
    }


    public MemberBiz getMemberBiz() {
        return memberBiz;
    }

    public void setMemberBiz(MemberBiz memberBiz) {
        this.memberBiz = memberBiz;
    }

    @Override
    public Member getModel() {
        if (member==null)
            member=new Member();
        return member;
    }
}
